package com.aibili.contoller.administrator;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aibili.common.JPushUtil;
import com.aibili.contoller.BaseController;
import com.aibili.pojo.FeedBack;
import com.aibili.pojo.MomentComment;
import com.aibili.pojo.MomentInfo;
import com.aibili.pojo.UserDevice;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.aibili.service.MomentService;
import com.aibili.service.UserDeviceService;
import com.aibili.service.UserInfoService;
import com.alibaba.fastjson.JSON;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

@Controller
@RequestMapping(value = "/administrator", produces = "application/json; charset=utf-8")
public class MomentController extends BaseController {

	@Resource
	private UserInfoService userInfoService;
	
	@Resource 
	private MomentService momentService;
	
	@Resource
	private UserDeviceService userDeviceService;
	
	/**
	 *
	 * @Description 评论列表
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "commentinfo", produces = "application/json; charset=utf-8")
	public ModelAndView commentInfo(Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<MomentComment> results = momentService.getCommentByParams(0, pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<MomentComment> comments = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", comments);
			mav.addObject("page", page);
		}
		return mav;
	}

	/**
	 *
	 * @Description 评论删除
	 * @param comment_id
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/commentdelete", produces = "application/json; charset=UTF-8")
	public String commentDelete(String comment_id, String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CommonUtils.isEmptyString(comment_id)) {
			MomentComment comment = momentService.getCommentById(CommonUtils.parseLong(comment_id, 0));
			if (comment != null) {
				comment.setIs_delete(CommonUtils.parseInt(type, 0));
				
				momentService.updateComment(comment);
				map.put("code", 0);
			} else {
				map.put("code", 1);
				map.put("message", "请选择删除对象");
			}
		} else {
			map.put("code", 1);
			map.put("message", "请选择删除对象");
		}

		return JSON.toJSONString(map);
	}
	
	/**
	 *
	 * @Description 动态列表
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "momentinfo", produces = "application/json; charset=utf-8")
	public ModelAndView momentInfo(Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<MomentInfo> results = momentService.getMomentByParams(0, pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<MomentInfo> comments = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", comments);
			mav.addObject("page", page);
		}
		return mav;
	}
	
	/**
	 *
	 * @Description 删除/警告操作
	 * @param prompt_type
	 * @param id
	 * @param prompt_message
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/promptmessage")
	public String promptMessage(Integer prompt_type, String id, String prompt_message) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CommonUtils.isEmptyString(id)) {
			MomentInfo info = momentService.getMonentInfo(CommonUtils.parseLong(id, 0));
			if (info != null) {
				if (prompt_type == 1) {
					info.setStatus(1);
				} else if (prompt_type == 2) {
					info.setStatus(2);
				}
				momentService.updateMomentInfo(info);
				// 发一则推送
				JPushUtil jpush = new JPushUtil();
				List<UserDevice> devices = userDeviceService.getByUserid(info.getUser().getId());
				for (UserDevice userDevice : devices) {
					if (userDevice.getType() == 0) {
						jpush.sendSingleAndroidPush(userDevice.getClient_id(), prompt_message);
					} else {
						jpush.sendSingleIOSPush(userDevice.getClient_id(), prompt_message, null);
					}
				}
			}
			map.put("code", 0);
		} else {
			map.put("code", 1);
		}
		return JSON.toJSONString(map);
	}
	
	/**
	 *
	 * @Description 动态详情
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/momentdetail")
	public ModelAndView momentDetail(String id) {
		ModelAndView mav = new ModelAndView();
		
		if (!CommonUtils.isEmptyString(id)) {
			MomentInfo info = momentService.getMonentInfo(CommonUtils.parseLong(id, 0));
			if (info != null) {
				mav.addObject("obj", info);
			}
		}
		
		return mav;
	}
	
	/**
	 *
	 * @Description 置顶
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/sticky")
	public String sticky(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		return JSON.toJSONString(map);
	}
	/**
	 * 反馈信息
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "feedbackinfo", produces = "application/json; charset=utf-8")
	public ModelAndView feedbackinfo(Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<FeedBack> results = momentService.getFeedbacklist(0, pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<FeedBack> feedback = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", feedback);
			mav.addObject("page", page);
		}
		return mav;
	}

	/**
	 * 加入精选
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/setchioce", produces = "application/json; charset=UTF-8")
	public String setchioce(String id) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		MomentInfo momentInfo = momentService.getMonentInfo(CommonUtils.parseLong(id, 0));
		if (momentInfo != null) {
			if (momentInfo.getChioce() == 0) {
				momentInfo.setChioce(1);
				momentInfo.setTop_time(new Date());
			} else {
				momentInfo.setChioce(0);
			}
			momentService.updateMomentInfo(momentInfo);
			map.put("code", 0);
		} else {
			map.put("code", 1);
			map.put("message", "该动态不存在！");
		}
		return JSON.toJSONString(map);
	}
	
	/**
	 * 获取精选列表
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "choicenessinfo", produces = "application/json; charset=utf-8")
	public ModelAndView choicenessinfo(Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<MomentInfo> results = momentService.getChioce(0, pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<MomentInfo> comments = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", comments);
			mav.addObject("page", page);
		}
		return mav;
	}
}
